mssql

推荐列表 站点导航

当前位置:首页 > 数据库 > mssql >

SQL Server设置主键自增长列(使用sql语句实现)

来源:网络整理  作者:网络  发布时间:2020-12-12 06:50
主键自增长列在进行数据插入的时候,很有用的,如可以获取返回的自增ID值,接下来将介绍SQL Server如何设置主键自增...

复制代码 代码如下:


if @Pk is not null


复制代码 代码如下:

alter table tb alter column id int not null


3.已经建好一数据表,里面有字段id,将id设为主键
create table tb(id int,constraint pkid primary key (id))

复制代码 代码如下:


复制代码 代码如下:

1.新建一数据表,里面有字段id,将id设为为主键


create table tb(id int identity(1,1) primary key )
create table tb(id int identity(1,1),constraint pkid primary key (id))

create table tb(id int primary key )
Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('tb') and xtype='PK';
2.新建一数据表,里面有字段id,将id设为主键且自动编号
Declare @Pk varChar(100);
exec('Alter table tb Drop '+ @Pk)

4.删除主键

alter table tb add constraint pkid primary key (id)

相关热词: SQL语句

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/sql/mssql/2834.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

SQL Server设置主键自增长列(使用sql语句实现)

2020-12-12 编辑:网络

复制代码 代码如下:


if @Pk is not null


复制代码 代码如下:

alter table tb alter column id int not null


3.已经建好一数据表,里面有字段id,将id设为主键
create table tb(id int,constraint pkid primary key (id))

复制代码 代码如下:


复制代码 代码如下:

1.新建一数据表,里面有字段id,将id设为为主键


create table tb(id int identity(1,1) primary key )
create table tb(id int identity(1,1),constraint pkid primary key (id))

create table tb(id int primary key )
Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('tb') and xtype='PK';
2.新建一数据表,里面有字段id,将id设为主键且自动编号
Declare @Pk varChar(100);
exec('Alter table tb Drop '+ @Pk)

4.删除主键

alter table tb add constraint pkid primary key (id)

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/sql/mssql/2834.shtml

相关文章

风云图片

推荐阅读

返回mssql频道首页